package com.wanmait.houlang.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import com.wanmait.houlang.pojo.Manager;

import java.util.Calendar;
import java.util.Date;

public class JWTUtils {
    /*
     签发对象：用户的id
     签发时间：现在
     有效时间：30分钟
     载荷内容：用户名和姓名，还可以是其它任何信息
     加密密钥：用户的密码
     */
    public static String createToken(Manager manager) {

        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE,30);
        Date expiresDate = nowTime.getTime();

        return JWT.create().withAudience(manager.getId().toString())   //签发对象
                .withIssuedAt(new Date())    //发行时间
                .withExpiresAt(expiresDate)  //有效时间
                .withClaim("adminId",manager.getId())//载荷
                .withClaim("username", manager.getManagerName())//载荷
//                .withClaim("name", manager.getName())//载荷
                .sign(Algorithm.HMAC256(manager.getManagerPass()));   //加密
    }

    //检验合法性，其中secret为用户的密码
    public static void verifyToken(String token, String secret)
    {
        DecodedJWT jwt = null;
        try {
            JWTVerifier verifier = JWT.require(Algorithm.HMAC256(secret)).build();
            jwt = verifier.verify(token);
        } catch (Exception e) {
            //效验失败
            throw new JWTVerificationException("token校验失败，非法认证信息");
        }

    }

   //获取签发对象也就是用户的id
    public static String getAudience(String token) {
        String audience=null;

        try {
            audience = JWT.decode(token).getAudience().get(0);

        } catch (JWTDecodeException j) {
            //这里是token解析失败
            throw new RuntimeException("token解析失败，非法认证信息");
        }
        return audience;
    }


    //通过载荷名字获取载荷的值
    public static Claim getClaimByName(String token, String name){
        return JWT.decode(token).getClaim(name);
    }
//手机验证码生成token
    public static String createTelToken(String code) {

        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE,30);
        Date expiresDate = nowTime.getTime();

        return JWT.create().withAudience(code)   //签发对象
                .withIssuedAt(new Date())    //发行时间
                .withExpiresAt(expiresDate)  //有效时间
                .withClaim("code",code)//载荷
//                .withClaim("username", manager.getManagerName())//载荷
//                .withClaim("name", manager.getName())//载荷
                .sign(Algorithm.HMAC256(code));   //加密
    }

    //通过载荷名字获取载荷的值
    public static Claim getClaimByCode(String token, String name){
        return JWT.decode(token).getClaim(name);
    }

    //获取签发对象也就是用户的id
    public static String getTelAudience(String token) {
        String audience=null;

        try {
            audience = JWT.decode(token).getAudience().get(0);

        } catch (JWTDecodeException j) {
            //这里是token解析失败
            throw new RuntimeException("token解析失败，非法认证信息");
        }
        return audience;
    }

}


